from plane_constant import *
from plane_sprites import GameSprite
from plane_bullet import Bullet1

class Hero(GameSprite):
    """英雄精灵"""

    def __init__(self, x_speed=0, y_speed=0):

        # 1. 调用父类方法，设置image&speed
        super().__init__("./images/me1.png", 0)
        self.image1 = pygame.image.load("images/me1.png")
        self.image2 = pygame.image.load("images/me2.png")
        self.destroy_images = []
        self.destroy_images.extend([pygame.image.load("images/me_destroy_1.png"),\
                                    pygame.image.load("images/me_destroy_2.png"), \
                                    pygame.image.load("images/me_destroy_3.png"), \
                                    pygame.image.load("images/me_destroy_4.png"), \
                                    ])

        self.active = True
        self.x_speed = x_speed
        self.y_speed = y_speed

        # 2. 设置英雄的初始位置
        self.rect.centerx = SCREEN_RECT.centerx
        self.rect.bottom = SCREEN_RECT.bottom - 120

        #3.生命数量
        self.life_image = pygame.image.load("images/life.png")
        self.life_rect = self.life_image.get_rect()
        self.life_num = 1

        # 4. 创建子弹的精灵组
        self.bullets = pygame.sprite.Group()

    def update(self):

        # 英雄在水平方向移动
        self.rect.x += self.x_speed
        self.rect.y += self.y_speed

        # 控制英雄不能离开屏幕
        if self.rect.x < 0:
            self.rect.x = 0
        elif self.rect.right > SCREEN_RECT.right:
            self.rect.right = SCREEN_RECT.right

        if self.rect.y < 0:
            self.rect.y = 0
        elif self.rect.bottom > SCREEN_RECT.bottom:
            self.rect.bottom = SCREEN_RECT.bottom

    def fire(self):
        print("发射子弹...")

        for i in (0, 1, 2):
            # 1. 创建子弹精灵
            bullet = Bullet1()

            # 2. 设置精灵的位置
            bullet.rect.bottom = self.rect.y - i * 20
            bullet.rect.centerx = self.rect.centerx

            # 3. 将精灵添加到精灵组
            self.bullets.add(bullet)

    def reset(self):
        self.rect.x, self.rect.y =SCREEN_RECT.right //2 - self.rect.width, \
                                     SCREEN_RECT.bottom
        self.active = True
